﻿using Pandora;
using Spine.Unity;
using UnityEditor;
using UnityEngine;

namespace PandoraEditor
{
    public static class ExecuteTool
    {
        private static readonly string BUILD_EXE = $"{Application.dataPath.ToLower().Replace("/assets", "")}/execute_tool/pandora.exe";
        private const string TITLE = "Pdr执行工具/";

        // [MenuItem(TITLE + "关联Lua依赖预制", false, 1000)]
        public static void ExecuteBuildExternal()
        {
            string actionName = "BindExternal";
            PdrCMDHelper.ProcessCommandEx(BUILD_EXE, $"{actionName} --input_path={Application.dataPath}");
            Debug.Log($"{actionName}运行完毕");
            AssetDatabase.Refresh();
        }

        [MenuItem(TITLE + "同步配置表+多语言", false, 1101)]
        public static void ExecuteSyncSvnData()
        {
            string path_data = $"{Application.dataPath}/Lua/game/LogicModule/data";
            PdrFileUtil.DeleteDirectory(path_data);
            SyncSvnData("lua", path_data);
            SyncSvnData("lua_code", $"{Application.dataPath}/Lua/game/LogicModule/constDef");
            SyncSvnData("language/lua", $"{Application.dataPath}/Lua/game/module/languages");
            AssetDatabase.Refresh();
        }

        [MenuItem(TITLE + "同步protobuf", false, 1102)]
        public static void ExecuteBuildLuaProtobuf()
        {
            string actionName = "BuildLuaProtobuf";
            var input_path = UserSetting.ProtocolSetting.git_proto_url;
            var output_path = PdrCombine.ProjectRoot(UserSetting.ProtocolSetting.path_output_lua_proto);
            var repo_path = UserSetting.ProtocolSetting.proto_repo;
            var git_branch = UserSetting.ProtocolSetting.git_branch;
            var cmd = $"{actionName} --input_path={input_path} --output_path={output_path} --repo_path={repo_path} --git_branch={git_branch}";
            PdrCMDHelper.ProcessCommandEx(BUILD_EXE, cmd);
            Debug.Log($"{actionName}运行完毕");
            AssetDatabase.Refresh();
        }

        private static void SyncSvnData(string source, string outputPath)
        {
            string actionName = "SyncSvnData";
            var inputPath = $"{UserSetting.VersionSetting.svn_url}/{UserSetting.VersionSetting.version_name}/{source}";
            var cmd = $"{actionName} --input_path={inputPath} --output_path={outputPath} --username {UserSetting.AccountSetting.svn_username} --password {UserSetting.AccountSetting.svn_password}";
            PdrCMDHelper.ProcessCommandEx(BUILD_EXE, cmd);
            Debug.Log($"{actionName}运行完毕");
            AssetDatabase.Refresh();
        }
    }
}